package com.aier.user.entity.userapp;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;



import net.eicp.ghostboy.futil.StringUtil;

import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;

public class CommidityRowMapper implements RowMapper<Commidity>{
    private int type;

//    public static final int Commidity_AND_STR = 1;

    public CommidityRowMapper() {
        
    }

    public CommidityRowMapper(int type) {
        this.type = type;
    }

    @Override
    public Commidity mapRow(ResultSet rs, int rowNum) throws SQLException {
        Commidity cd = new Commidity();
//        switch (type) {
//            case Commidity_AND_STR:
//                bz = parseCommidity(new Commidity(), rs);
//                break;
//            default:
//                bz = parseCommidity(new Commidity(), rs);
//        }
        cd = parseCommidity(new Commidity(), rs);
        return cd;
    }

    public static Commidity parseCommidity(Commidity cd, ResultSet rs)
        throws SQLException {
        return parseCommidity(cd, rs, null);
    }

    public static Commidity parseCommidity(Commidity cd, ResultSet rs, String su)
        throws SQLException {

        if (StringUtil.isStringNull(su)) {
            su = "";
        } else {
            su += ".";
        }

        try {
            cd.setCid(rs.getLong(su + "cid"));
        } catch (SQLException e) {
            cd.setCid(null);
        }
        try {
            cd.setMid(rs.getLong(su + "mid"));
        } catch (SQLException e) {
            cd.setMid(null);
        }
        try {
            cd.setCname(rs.getString(su + "cname"));
        } catch (SQLException e) {
            cd.setCname(null);
        }
        try {
            cd.setDescription(rs.getString(su + "description"));
        } catch (SQLException e) {
            cd.setDescription(null);
        }
        try {
            cd.setPrice(rs.getInt(su + "price"));
        } catch (SQLException e) {
            cd.setPrice(null);
        }

        
        return cd;
    }

    public static Map<String, Object> getPar(Map<String, Object> par,
            Commidity cd) {
        par.put("cid", cd.getCid());
        par.put("mid", cd.getMid());
        par.put("cname", cd.getCname());
        par.put("description", cd.getDescription());
        par.put("price", cd.getPrice());
        return par;
    }

    public static Map<String, Object> getArgs(Map<String, Object> args,
            Commidity cd) {
        args.put("cid", cd.getCid());
        return args;
    }

    /**
     * 
     * 
     * @param tfp
     * @return
     */
    public static PreparedStatementCreator getPreparedStatementCreator(
            final Commidity cd) {
        return new PreparedStatementCreator() {
            @Override
            public PreparedStatement createPreparedStatement(Connection con)
                throws SQLException {
                PreparedStatement ps = con.prepareStatement(parseSQL(cd),
                        new String[] { "cid" });
                parsePS(ps, cd);
                return ps;
            }

        };
    }

    private static String parseSQL(Commidity cd) throws SQLException {
        StringBuffer sql = new StringBuffer("insert into userapp_commidity(");
        int count = 0;
        if (null != cd.getCid()) {
            sql.append("cid,");
            count++;
        }
        if (null != cd.getMid()) {
            sql.append("mid,");
            count++;
        }
        if (null != cd.getCname()) {
            sql.append("cname,");
            count++;
        }
        if (null != cd.getDescription()) {
            sql.append("description,");
            count++;
        }
        if (null != cd.getPrice()) {
            sql.append("price,");
            count++;
        }

        
        sql = new StringBuffer(sql.substring(0, sql.length() - 1));
        sql.append(") value(");
        for (int i = 0; i < (count - 1); i++) {
            sql.append("?,");
        }
        return sql.append("?)").toString();
    }

    private static void parsePS(PreparedStatement ps, Commidity cd)
        throws SQLException {
        int count = 1;
        if (null != cd.getCid()) {
            ps.setLong(count++, cd.getCid());
        }
        if (null != cd.getMid()) {
            ps.setLong(count++, cd.getMid());
        }
        if (null != cd.getCname()) {
            ps.setString(count++, cd.getCname());
        }  
        if (null != cd.getDescription()) {
            ps.setString(count++, cd.getDescription());
        } 
        if (null != cd.getPrice()) {
            ps.setInt(count++, cd.getPrice());
        } 

    }

}
